package com.swim.common.utils;


import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import java.net.URLEncoder;

/**
 * @Author tql
 * @Description 短信发送
 * @Date 21-3-8 上午10:11
 * @Version 1.0
 */
@Slf4j
public class SmsUtil {
    // 你的appkey
    public static final String APPKEY = "9a5491484e40d3ab";
    public static final String URL = "https://api.jisuapi.com/sms/send";
    // 手机号
//    public static final String mobile = "15370142983";

    public static void Get(String mobile, String code) throws Exception {
        // utf-8
        String result = null;
        String content = "尊敬的会员，您的验证码:" + code + "。您正在注册，10分钟内有效。【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        log.info("短信发送地址：{}", url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                log.error("调用短信接口返回失败:{}", json);
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                log.info("count={},accuntId={}", count, accountid);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static void forgetPwd(String mobile, String code) throws Exception {
        // utf-8
        String result = null;
        String content = "尊敬的会员，您的验证码:" + code + "。您正在注册，10分钟内有效。【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户下单后给用游泳用户发送核销码
     *
     * @param orderNum 订单号
     * @param mobile   手机号
     * @param code     核销码
     * @throws Exception
     */
    public static void sendSettlementCode(String orderNum, String mobile, String code) throws Exception {
        // utf-8
        String result = null;
        String content = "尊敬的会员，您的订单号:" + orderNum + "对应的核销码是" + code + "。请凭核销码到游泳馆享受服务。【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            System.out.println("核销码短信发送结果" + json.toString());
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户下单后通知游泳馆
     *
     * @param productName 产品名称
     * @param mobile      手机号
     * @throws Exception
     */
    public static void placeOrderNoticeNatatorium(String productName, String mobile) throws Exception {
        // utf-8
        String result = null;
        String content = "下单通知，有用户购买" + productName + "产品，请知悉。 【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            System.out.println("用户下单后通知游泳馆短信发送结果" + json.toString());
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户下单后通知平台
     *
     * @param natatoriumName 场馆名称
     * @param productName    产品名称
     * @param mobile         手机号
     * @throws Exception
     */
    public static void placeOrderNoticePlatform(String natatoriumName, String productName, String mobile) throws Exception {
        // utf-8
        String result = null;
        String content = "下单通知，" + natatoriumName + "场馆有用户购买" + productName + "产品，请知悉。 【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            System.out.println("用户下单后通知平台短信发送结果" + json.toString());
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户下单后通知游泳馆
     *
     * @param productName 产品名称
     * @param mobile      手机号
     * @throws Exception
     */
    public static void sendSettlementCode(String productName, String mobile) throws Exception {
        // utf-8
        String result = null;
        String content = "下单通知，有用户购买" + productName + "产品，请知悉。 【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            System.out.println("用户下单后通知游泳馆短信发送结果" + json.toString());
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 预约上课信息发送短信给李教练
     *
     * @param mobile              手机号
     * @param reservationCoachMsg 预约上课信息
     * @throws Exception
     */
    public static void sendReservationCoach(String mobile, String reservationCoachMsg) throws Exception {
        String result = null;
        String content = "有新学员预约上课啦，学员信息如下：" + reservationCoachMsg + "【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 游泳馆申请入驻提交成功后发送短信给用户
     *
     * @param mobile         手机号
     * @param natatoriumName 游泳馆名称
     * @throws Exception
     */
    public static void sendNatatoriumApply(String mobile, String natatoriumName) throws Exception {
        String result = null;
        String content = natatoriumName + "入驻申请提交成功，平台会在三个工作日内进行信息审核，请您手机保持畅通，客服会联系您沟通入驻事项【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 游泳馆申请入驻发送短信给李教练
     *
     * @param mobile         手机号
     * @param natatoriumName 游泳馆名称
     * @throws Exception
     */
    public static void sendNatatoriumApplyByLi(String mobile, String natatoriumName) throws Exception {
        String result = null;
        String content = "有游泳馆申请入驻啦，游泳馆名称为：" + natatoriumName + "【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 私教入驻发送短信给李教练
     *
     * @param mobile 手机号
     * @param name   私教姓名
     * @throws Exception
     */
    public static void sendCoachByLi(String mobile, String coachName, String coachMobile) throws Exception {
        String result = null;
        String content = "有私教申请入驻啦，私教姓名是：" + coachName + "，私教手机号是：" + coachMobile + "【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        System.out.println("接口地址" + url);
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户在商城下单后通知李教练
     *
     * @param productName 产品名称
     * @param mobile      手机号
     * @throws Exception
     */
    public static void mallOrderNotification(String productName, String mobile) throws Exception {
        String result = null;
        String content = "平台商城下单通知，" + productName + "产品有用户购买了，请及时处理。【苏州游泳培训网】";
        String url = URL + "?mobile=" + mobile + "&content=" + URLEncoder.encode(content, "utf-8") + "&appkey=" + APPKEY;
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json);
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                String count = resultarr.getString("count");
                String accountid = resultarr.getString("accountid");
                System.out.println(count + " " + accountid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
